//
//  AELAddWeatherController.swift
//  ActualExaminationLine
//
//  Created by iOS-Czz on 2023/10/29.
//

import UIKit

class AELAddWeatherController: AELBaseViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wx_navigationBar.isHidden = true
    }
    @IBOutlet var thunderCoverView: UIView!
    @IBOutlet var cloudyCoverView: UIView!
    @IBOutlet var clearUpCoverView: UIView!
    @IBOutlet var overcastCoverView: UIView!
    @IBOutlet var spitCoverView: UIView!
    
    @IBOutlet var thunderLabel: UILabel!
    @IBOutlet var cloudyLabel: UILabel!
    @IBOutlet var clearUpLabel: UILabel!
    @IBOutlet var overcastLabel: UILabel!
    @IBOutlet var spitLabel: UILabel!
    
    
    @IBOutlet var locationTF: UITextField!
    @IBOutlet  var temperatureTF: UITextField!
    @IBOutlet var weekTF: UITextField!
    @IBOutlet var dateTF: UITextField!
    
    var covers:[UIView] = []
    var weathers:[UILabel] = []
    var weather = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        self.covers = [self.thunderCoverView,self.cloudyCoverView,self.clearUpCoverView,self.overcastCoverView,self.spitCoverView]
        self.weathers = [self.thunderLabel,self.cloudyLabel,self.clearUpLabel,self.overcastLabel,self.spitLabel]
        self.tapClearUpAction(self.clearUpCoverView)
        // Do any additional setup after loading the view.
    }
    @IBAction func clickBackAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func clickSaveAction(_ sender: Any) {
        self.view.endEditing(true)
        if self.dateTF.text!.isEmpty {
            UIView.showTipsView("Please select the date")
            return
        }
        if self.temperatureTF.text!.isEmpty {
            UIView.showTipsView("Please enter temperature")
            return
        }
        if self.locationTF.text!.isEmpty {
            UIView.showTipsView("Please enter location")
            return
        }
        let model = AELWeatherModel()
        model.weather = self.weather
        model.date = self.dateTF.text!
        model.week = self.weekTF.text!
        model.temperature = self.temperatureTF.text! + "℃"
        model.location = self.locationTF.text!
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView("Add weather succeed", true)
        let helper = AELWeatherModel.getUsingLKDBHelper()
        helper.delete(with: AELWeatherModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    @IBAction func tapThunderAction(_ sender: UIView) {
        self.weather = 1
        changeView(self.thunderCoverView)
        changeLabel(self.thunderLabel)
    }
    @IBAction func tapCloudlyAction(_ sender: UIView) {
        self.weather = 2
        changeView(self.cloudyCoverView)
        changeLabel(self.cloudyLabel)
    }
    @IBAction func tapClearUpAction(_ sender: UIView) {
        self.weather = 0
        changeView(self.clearUpCoverView)
        changeLabel(self.clearUpLabel)
    }
    @IBAction func tapOvercastAction(_ sender: UIView) {
        self.weather = 3
        changeView(self.overcastCoverView)
        changeLabel(self.overcastLabel)
    }
    @IBAction func tapSpitAction(_ sender: UIView) {
        self.weather = 4
        changeView(self.spitCoverView)
        changeLabel(self.spitLabel)
    }
    func changeView(_ view:UIView){
        for item in self.covers {
            if item == view {
                item.alpha = 0
            }else{
                item.alpha = 0.4
            }
        }
    }
    func changeLabel(_ label:UILabel){
        for item in self.weathers {
            if item == label {
                item.isHidden = false
            }else{
                item.isHidden = true
            }
        }
    }
    @IBAction func tapSelectTheDateAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wDateTimeTypeSet()("yyyy.MM.dd")
        .wDefaultDateSet()(Date())
        .wCancelTitleSet()("Cancel")
        .wOKTitleSet()("Ok")
        .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
            self.dateTF.text = otherData as? String
            if let dayOfWeek = self.dateTF.text!.dayOfWeek() {
                self.weekTF.text = dayOfWeek
            } else {
                self.weekTF.text = ""
            }
        })
        .wTypeSet()(DialogTypeDatePicker);
        _ = dialog.wStart()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
